package com.haibara.codesubmiteval.runner;

import com.haibara.codesubmiteval.chain.redisRunnerChain.RedisInitContext;
import com.haibara.codesubmiteval.chain.redisRunnerChain.RedisInitHandler;
import com.haibara.codesubmiteval.mapper.QuestionMapper;
import com.haibara.codesubmiteval.mapper.UserMapper;
import com.haibara.codesubmiteval.model.entity.User;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Redis初始化
 */
@Component
@Slf4j
public class RedisDataSyncRunner implements CommandLineRunner {

    @Resource
    private UserMapper userMapper;
    @Resource
    private QuestionMapper questionMapper;
    @Resource
    private RedisInitHandler redisInitHandlerHeader;

    @Override
    public void run(String... args) {
        // 从数据库读取数据
        List<User> users = userMapper.selectList(null);
        int questionNumber = questionMapper.getQuestionNumber();
        RedisInitContext context = RedisInitContext.builder()
                .users(users)
                .questionNumber(questionNumber)
                .build();
        // 责任链完成Redis初始化
        redisInitHandlerHeader.handleRedisInit(context);
        log.info("Redis初始化成功");
    }
}